package com.atguigu.app;

import com.atguigu.bean.Bean1;
import com.atguigu.bean.Bean2;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.ProcessJoinFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

/**
 * @Author:GaoFei
 * @Description:
 * @Date:Created in 18:11
 * @Modified By:
 */
public class StreamJoin {
    public static void main(String[] args) throws Exception {
        // 获取环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<Bean1> s1 = env.socketTextStream("192.168.10.100", 8888)
                .map(f -> {
                    String[] split = f.split(",");
                    return new Bean1(split[0], split[1], Long.valueOf(split[2]));
                }).assignTimestampsAndWatermarks(WatermarkStrategy
                        .<Bean1>forMonotonousTimestamps()
                        .withTimestampAssigner(new SerializableTimestampAssigner<Bean1>() {
                            @Override
                            public long extractTimestamp(Bean1 element, long recordTimestamp) {
                                return element.getTs() * 1000L;
                            }
                        }));

        SingleOutputStreamOperator<Bean2> s2 = env.socketTextStream("192.168.10.100", 9999)
                .map(f -> {
                    String[] split = f.split(",");
                    return new Bean2(split[0], split[1], Long.valueOf(split[2]));
                }).assignTimestampsAndWatermarks(WatermarkStrategy
                        .<Bean2>forMonotonousTimestamps()
                        .withTimestampAssigner(new SerializableTimestampAssigner<Bean2>() {
                            @Override
                            public long extractTimestamp(Bean2 element, long recordTimestamp) {
                                return element.getTs() * 1000L;
                            }
                        }));

        SingleOutputStreamOperator<Tuple2<Bean1, Bean2>> process = s1.keyBy(Bean1::getId)
                .intervalJoin(s2.keyBy(Bean2::getId))
                .between(Time.seconds(-5), Time.seconds(5))
//                   左开右闭
//                .lowerBoundExclusive()
//                   左闭右开
//                .upperBoundExclusive()
                .process(new ProcessJoinFunction<Bean1, Bean2, Tuple2<Bean1, Bean2>>() {
                    @Override
                    public void processElement(Bean1 bean1, Bean2 bean2, Context context, Collector<Tuple2<Bean1, Bean2>> collector) throws Exception {
                        collector.collect(Tuple2.of(bean1, bean2));
                    }
                });
        process.print();
        env.execute();
    }
}
